
var TopCourseArray = <?=$type?>;
var JsGetCourse = new Object();
var JsGetChapter = new Object();
$(function(){
    $('#topcourse :input').click(function(){
        if($(this).attr('checked')){
            var topcourseid = $(this).val();
            $('#thirdcoursecontenuer').slideUp('normal',function(){
                $('#subcoursecontenuer').slideUp('normal',function(){
                    $('#subcourse_ul').empty();
                    $('#thirdcourse').html('<option value="-1">Please Select...</option>');
                    switch(TopCourseArray[topcourseid]['deep']) {
                        case '3':
                            var postJsondata ={"course_type":topcourseid};
							var courseArray = '';							
							JsGetCourse.response = function(res){
								courseArray = res.data;
							}
						    $.ajax({
								 type:'POST',
								 url:'js_getCourse.html',
								 data:postJsondata,
								 dataType: 'json',
								 success:JsGetCourse.response,
								 async:false
							});
                            $.each(courseArray,function(k,v){
           $('#subcourse_ul').append('<li><input type="radio" name="course" topcourseid ="'+topcourseid+'" value="'+k+'" />&nbsp;'+ v.coursename+'</li>');
                            });
                            $('#subcoursecontenuer').slideDown();break;
                        case '2':
							var postJsondata ={"course_type":topcourseid,"deep":"2"};
							var chapterArray = '';	
							var course_name ='';								
							JsGetChapter.response = function(res){
								chapterArray = res.data;
								course_name = res.course_name;
							}
						    $.ajax({
								 type:'POST',
								 url:'js_getChapter.html',
								 data:postJsondata,
								 dataType: 'json',
								 success:JsGetChapter.response,
								 async:false
							});
							 $('#thirdcoursetitle').html(course_name);
                            $.each(chapterArray,function(k,v){
                                $('#thirdcourse').append('<option value="'+k+'">'+ v.chaptername+'</option>');
                            });
                            $('#thirdcoursecontenuer').slideDown();break;
                        default:
                            return;
                    }
                });
            });
        }
    });
    
    $('#subcourse_ul :input').live('click',function(){
        var topcourseid = $(this).attr('topcourseid');
        var subcourseid = $(this).val();
		var postJsondata ={"course_id":subcourseid};
		var chapterArray = '';	
		var course_name ='';						
		JsGetChapter.response = function(res){
			chapterArray = res.data;
			course_name = res.course_name;
		}
	    $.ajax({
			 type:'POST',
			 url:'js_getChapter.html',
			 data:postJsondata,
			 dataType: 'json',
			 success:JsGetChapter.response,
			 async:false
		});        

        $('#thirdcoursecontenuer').slideUp('normal',function(){
            if(chapterArray!==''){
                $('#thirdcourse').html('<option value="-1">Please Select...</option>');
                $('#thirdcoursetitle').html(course_name);
                $.each(chapterArray,function(k,v){
                    $('#thirdcourse').append('<option value="'+k+'">'+ v.chaptername+'</option>');
                });
                $('#thirdcoursecontenuer').slideDown();
            }
        });
    })
    
    $('#thirdcourse').change(function(){
        if($(this).val() != -1){
            $('#view_course').show();
        }else{
            $('#view_course').hide();
        }
    });
    
    $('#Desc').focus(function(){
        $(this).val('').css('color','#000000');
    });

    $('#view_course').click(function(){
        var firstCourseId = '';
        $("input[name='course_type_radio']").each(  
			    function(){  
			    	var checked = $(this).attr("checked");
			    	if(checked=='checked'){
			    		firstCourseId = $(this).val();
			    	}
			    }   
	 	)
	 	
        var secondCourseId = $('input[name="SubCourse"]').val();
        var thirdCourseId = $('#thirdcourse').val();
        var myform = $('#view_course_form')
        myform.attr('action','user_downloadMaterial.html').html('<input type="text" name="type" value="'+firstCourseId+'"><input type="text" name="course" value="'+secondCourseId+'"><input type="text" name="chapter" value="'+thirdCourseId+'">');
        myform.submit();
});
});
